<?php

namespace app\common\service\pay;

use app\api\controller\v2\Pay;
use app\common\facade\auth\Auth;
use app\model\CouponOrder;
use app\model\PayLog;
use think\Exception;
use think\helper\Str;

/**
 * @property int $id 自增ID
 * @property int $agent_id 代理商id
 * @property int $channel_id 渠道id
 * @property int $user_id 用户ID
 * @property int $pay_channel 支付渠道：1微信支付 2支付宝支付
 * @property int $order_id 订单号
 * @property string $trade_no 第三方支付流水号
 * @property string $trade_type 交易类型：MWEB - H5支付，JSAPI支付
 * @property string $mobile 手机号
 * @property int $amount 充值金额。单位：分
 * @property float $sale_discount 销售折扣
 * @property int $sale_price 实际支付金额。单位：分
 * @property float $settle_discount 结算折扣
 * @property int $settle_price 结算价。单位：分
 * @property int $real_pay_price 实际支付金额：微信回调金额。单位：分
 * @property int $status 状态：1未支付 2已支付 3已取消 4支付失败
 * @property string $remark 充值备注
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property int $settle_type 结算方式：1返佣 2预付
 * @property int $refund_amount 已退款总金额【退给用户的钱】。单位：分
 * @property int $refund_settle_amount 退款总金额【退款给代理商的 预充的才需要】。单位：分
 * @property int $type 类型：1话费充值 2代理商充值
 * @property int $title 支付标题
 * @property int $out_no 出口编号
 */
class Base
{

    public $refund_number;//商家退单号


    /**
     * @var CouponOrder $couponOrder
     */
    public $couponOrder;


    /**
     * @var PayLog $payLog
     */
    public $payLog;



    /**
     * @param CouponOrder $couponOrder
     */
    public function init(CouponOrder $couponOrder)
    {
        $this->couponOrder = $couponOrder;

        if ($this->couponOrder->pay_status == CouponOrder::C_PAY_STATUS_YES) {
            throw new Exception('订单已支付，请勿重复提交');
        }

        //创建pay_log

        $this->payLog = new PayLog($this->getFormatData());

        $this->payLog->save();

        return $this;
    }

    /**
     * 获取属性
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws Exception
     */
    public function __get($name)
    {

        $method_name = 'get' . Str::studly($name);

        if (!method_exists($this, $method_name)) throw new Exception('渠道未配置' . $name);

        return $this->$method_name();
    }

    public function getAgentId()
    {
        return $this->couponOrder->agent_id;
    }


    public function getChannelId()
    {
        return $this->couponOrder->cnid;
    }

    public function getUserId()
    {
        return $this->couponOrder->user_id;
    }

    public function getOrderId()
    {
        return $this->couponOrder->order_id;
    }

    public function getPayChannel()
    {
        return $this->pay_channel;
    }

    public function getTradeType()
    {
        return $this->trade_type;
    }

    /**
     * 手机号
     */
    public function getMobile()
    {
        return $this->couponOrder->mobile;
    }

    /**
     * 金额
     */
    public function getAmount()
    {
        return $this->couponOrder->pay_amount;
    }

    public function getSaleDiscount()
    {
        return 0;
    }

    /**
     * 实际销售价
     * @return int
     */
    public function getSalePrice()
    {
        return $this->amount;
    }

    /**
     * 结算金额
     */
    public function getSettlePrice()
    {

        return $this->couponOrder->settle_price;
    }

    /**
     * 真实结算金额
     * @return int
     */
    public function getRealPayPrice()
    {
        return $this->amount;
    }


    /**
     * 支付狀態
     * @return int
     */
    public function getStatus()
    {
        return PayLog::STATUS_NO;
    }

    /**
     * 备注
     * @return string
     */
    public function getRemark()
    {
        return '';
    }

    /**
     * 结算类型
     * @return string
     */
    public function getSettleType()
    {
        return $this->couponOrder->settle_type;
    }

    public function getType()
    {
        return $this->couponOrder->serv_type;
    }

    /**
     * 支付标题
     * @return string
     */
    public function getTitle()
    {
        return $this->couponOrder->product_name;
    }

    public function getOutNo()
    {

        if (isLocal()) {
            return date('YmdHis') . rand(1000009, 9999999) . 'test';
        }

        return date('YmdHis') . rand(1000009, 9999999);
    }


    /**
     * 格式化数据
     */
    public function getFormatData()
    {
        /**
         * @var PayLog
         */
        return [
            'agent_id' => $this->agent_id,
            'channel_id' => $this->channel_id,
            'user_id' => $this->user_id,
            'order_id' => $this->order_id,
            'pay_channel' => $this->pay_channel,
            'trade_type' => $this->trade_type,
            'mobile' => $this->mobile,
            'amount' => $this->amount,
            'sale_discount' => $this->sale_discount,
            'settle_price' => $this->settle_price,
//            'real_pay_price' => $this->real_pay_price,
            'status' => $this->status,
            'remark' => $this->remark,
            'settle_type' => $this->settle_type,
            'type' => $this->type,
            'out_no' => $this->out_no,
        ];
    }

    /**
     * @param CouponOrder $couponOrder
     */
    public function setCouponOrder(CouponOrder $couponOrder)
    {
        $this->couponOrder = $couponOrder;

        return $this;
    }

}